<?php

namespace App\Services\ThreeDMap;

use App\Models\ThreeDMap\PanoramaModel;
use App\Tools\Common;
use Illuminate\Support\Facades\DB;

class PanoramaService
{


    /**
     * 获取全部
     * @param $data
     * @return array
     */
    public function all($data)
    {

        // 查询条件
        $result =
            PanoramaModel::when(@$data['select'], function ($q) use ($data) {
                $q->selectRaw($data['select']);
            })
                ->when(@$data['guid'], function ($q) use ($data) {
                    $q->where('guid', $data['guid']);
                })
                ->when(@$data['map_id'], function ($q) use ($data) {
                    $q->where('map_id', $data['map_id']);
                })
                ->when(@$data['name'], function ($q) use ($data) {
                    $q->where('name', 'like', '%' . $data['name'] . '%');
                })
                ->when(@$data['status'], function ($q) use ($data) {
                    $q->where('status', $data['status']);
                })
                ->orderBy('created_at', 'desc')
                ->get();

        return ['status' => true, 'data' => $result];
    }

    /**
     * 获取列表
     * @param $data
     * @return array
     */
    public function list($data)
    {
        $current = $data['current'] ?? 1;
        $pageSize = $data['pageSize'] ?? 10;

        // 查询条件
        $paginate = PanoramaModel::select('*')
            ->when(@$data['guid'], function ($q) use ($data) {
                $q->where('guid', $data['guid']);
            })
            ->when(@$data['map_id'], function ($q) use ($data) {
                $q->where('map_id', $data['map_id']);
            })
            ->when(@$data['code_id'], function ($q) use ($data) {
                $q->where('code_id', $data['code_id']);
            })->when(@$data['status'], function ($q) use ($data) {
                $q->where('status', $data['status']);
            })
            ->when(@$data['name'], function ($q) use ($data) {
                $q->where('name', 'like', '%' . $data['name'] . '%');
            })
            ->orderBy('created_at', 'desc')
            ->forPage($current, $pageSize)
            ->paginate($pageSize, ['*'], 'page', $current);

        return [
            'status' => true,
            'data' => [
                'current' => $current,
                'data' => $paginate->items(),
                'pageSize' => $pageSize,
                'total' => $paginate->total(),
            ]
        ];
    }

    /**
     * 添加
     * @param $data
     * @return array
     */
    public function add($data)
    {

        $checkTitle = PanoramaModel::where('name', $data['name'])->where('map_id', $data['map_id'])->first();
        if ($checkTitle) {
            return ['status' => false, 'data' => '名称重复!', 'code' => 41064];
        }

        $checkCode = PanoramaModel::where('code_id', $data['code_id'])->first();
        if ($checkCode) {
            return ['status' => false, 'data' => '二维码标识重复!', 'code' => 41064];
        }


        $id = Common::getUuid();
        $result = PanoramaModel::insert([
            'guid' => $id,

            'name' => $data['name'], // 名称

            'url' => $data['url'], // 网址

            'map_id' => $data['map_id'], // 地图id

            'code_id' => $data['code_id'], // 地图id

            'icon' => @$data['icon'], // 图标或者封面

            'coordinates'=> @$data['coordinates'] ? json_encode( @$data['coordinates']):null,
            'created_at' => time()
        ]);


        if (!$result) {
            return ['status' => false, 'data' => '添加失败!', 'code' => 41044];
        }

        return ['status' => true, 'data' => $result];
    }

    /**
     * 更新
     * @param $guid
     * @param $data
     * @return array
     */
    public function update($guid, $data)
    {
        if (@$data['name']) {
            $checkTitle = PanoramaModel::where(['name' => $data['name'], ['guid', '<>', $guid]])->where('map_id', $data['map_id'])->first();
            if ($checkTitle) {
                return ['status' => false, 'data' => '名称重复!', 'code' => 41064];
            }
            $updateParams['name'] = $data['name'];
        }
        if (@$data['code_id']){
            $checkCode = PanoramaModel::where(['code_id' => $data['code_id'], ['guid', '<>', $guid]])->first();
            if ($checkCode) {
                return ['status' => false, 'data' => '二维码标识重复!', 'code' => 41064];
            }
            $updateParams['code_id'] = $data['code_id'];
        }
        if (@$data['status']) {
            $updateParams['status'] = $data['status'];
        }else{
            if (@$data['url']) $updateParams['url'] = $data['url'];
            if (@$data['map_id']) $updateParams['map_id'] = $data['map_id'];
            if (@$data['icon']) $updateParams['icon'] = $data['icon'];
            $updateParams['coordinates'] =@$data['coordinates']? json_encode($data['coordinates']):null ;

        }

        $result = PanoramaModel::where('guid', $guid)->update($updateParams);

        if (!$result) {
            return ['status' => false, 'data' => '数据不存在或者处于上架状态，修改失败', 'code' => 41044];
        }

        return ['status' => true, 'data' => $result];
    }

    /**
     * 删除
     * @param $guid
     * @return array
     */
    public function delete($guid): array
    {
        DB::beginTransaction();
        try {
            $result = PanoramaModel::where('guid', $guid)->where('status', 2)->delete();
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            return ['status' => false, 'data' => $exception->getMessage(), 'code' => 41044];
        }

        return ['status' => true, 'data' => $result];
    }


    // 获取场景信息
    public function getPanoramaInfo($guid)
    {
        if (strlen($guid) == 6){
            $data = PanoramaModel::where('code_id', $guid)->where('status', 1)->with('infoSpot')->first();
        }else{
            $data = PanoramaModel::where('guid', $guid)->where('status', 1)->with('infoSpot')->first();
        }
        if (empty($data)) {
            return ['status' => false, 'data' => '数据不存在!', 'code' => 41064];
        }
//        $list =json_decode(json_encode($data->infoSpot),true) ;
//        $data->preload = PanoramaModel::whereIn('guid', array_column($list, 'to_panorama_id'))->with('infoSpot')->get();

        return ['status' => true, 'data' => $data];
    }

}
